pageController = function(){
    
    
    // ************ HISTORY STATE
    
    this.checkHash = function() {
        var hash =  window.location.hash;
        hash = hash.replace(new RegExp("http://"+window.location.hostname,""),"");
        if(hash.indexOf('!/') != -1) {
            new_url = hash.replace(new RegExp("#!",""),"");
            window.location.replace(new_url);
        }
    };
    this.initHistory = function() {
        $.history.init(function(url) {
            url = url.replace(new RegExp("!",""),"");
            pageController.ajaxLoadPage(url);
        });
        $('a.ajax-load').live('click', function() {
            var url = $(this).attr('href');
            $.history.load(url);
            return false;
        });
    };
    
    // ********************* PAGE LOAD
    
    this.ajaxLoadPage = function(url) {
        if(url == '') {
            url = window.location.pathname;
        }
        commonController.showLoader('#content');
        $.ajax({
            url: url,
            type: 'get',
            dataType: 'json',
            data: {},
            timeout: 12000,
            async: false,
            error: function() {
                commonController.showConnectionError()
                commonController.hideLoader('#content');
                window.location.href=url;
            },
            success: function(data) {
                if((typeof(data.errors) != 'undefined') && (data.errors != '')) {
                    commonController.showErrorMessage(data.errors);
                    window.location.href=url;
                } else {
                    if(typeof(data.html) != 'undefined') {
                        $('#content').html(data.html);
                        $('head title').html(data.title);
                        pageController.setBgClass(data.bg_class);
                        commonController.replaceFont();
                        pageController.initMenuHovers();
                        if(data.bg_class=='bg_blog-comments')
                            $('#scrollbar1').tinyscrollbar();
                    }
                }
                commonController.hideLoader('#content');
            }
        });
        return false;
    };
    
    
    
    // ************** PAGE STYLE
    
    this.setBgClass = function(bg_class) {
        $('body').removeClass($('body').attr('class'))
        .addClass(bg_class);
    };
    this.initMenuHovers = function() {
            $('.left li, .right li, .bottom-right li, .articles li')
    .find('a').each(function(){
        var section_desc = $(this).find('.section-desc').html();
        if(section_desc == null) {
            section_desc = '';
        }
        $(this).find('.section-desc').remove();
        $(this).removeClass('highlight')
        .append('<div class="hover"><span class="hover-section-desc hidden">'+section_desc+'</span></div>').each(function () {
            var $span = $('> div.hover', this).css('opacity', 0);
            $(this).hover(function () {
                // on hover
                $span.stop().fadeTo(500,1, function(){
                    $(this).find('.hover-section-desc').show();
                });
            }, function () {
                //off hover
                $span.stop().fadeTo(500,0, function(){
                    $(this).find('.hover-section-desc').hide();
                });
            });
        });
    });
    
    if($('.reviews_blocks li').html()) {
        $('.reviews_blocks li')
        .find('a').each(function(){
            var review_text = $(this).find('.reviews_text');
            if(review_text == null) {
                review_text = '';
            }
            $(this).removeClass('highlight')
            .append('<div class="hover"></div>').each(function () {
                var $hover_div = $('> div.hover', this).css('opacity', 0);
                $(review_text).clone().appendTo($hover_div);
                $(this).hover(function () {
                    // on hover
                    $hover_div.stop().fadeTo (500,1, function(){
                        $(this).find('.hover-section-desc').show();
                    });
                }, function () {
                    //off hover
                    $hover_div.stop().fadeTo(500,0, function(){
                        $(this).find('.hover-section-desc').hide();
                    });
                });
            });
        });
    } 
    };
    this.initCustomSelectboxes = function() {
        $('.custom-sb').sb({
            fixedWidth: true,
            maxWidth: 457
        });
    };
    
    this.pushHistoryState = function(title, url) {
        //        if (typeof(window.history.pushState) == 'function'){
        //            window.history.pushState(null,title,url);
        //        } else {
        $('head title').html(title);
        window.location.hash = '!'+url;
    //
    };
    this.sendForm=function(form_id){
         formSelector='#'+form_id;
         form = $(formSelector);
        commonController.showLoader(formSelector);
        $.ajax({
            url: $(form).attr('action'),
            type: 'post',
            dataType: 'json',
            data: $(form).serialize(),
            timeout: 12000,
            async: false,
            error: function() {
                commonController.showConnectionError();
                commonController.hideLoader(formSelector);
            },
            success: function(data) {
                commonController.hideLoader(formSelector);
                if((typeof(data.errors) != 'undefined') && (data.errors != '')) {
                    if((typeof(data.csrf_token) != 'undefined') && (data.csrf_token != '')) {
                        $('#csrf_token').val(data.csrf_token);
                    }
                    commonController.showErrorMessage(data.errors);
                } else {
                   if(data.html!='undefined'&&data.html){
                        $('#content').html(data.html);
                        $('head title').html(data.title);
                        $.history.load(data.url);
                        pageController.setBgClass(data.bg_class);
                        commonController.replaceFont();
                        if(data.bg_class=='bg_blog-comments')
                            $('#scrollbar1').tinyscrollbar();
                    }
                }
            }
        });
        return false;
    }
};

var pageController = new pageController();
pageController.checkHash();
$(document).ready(function(){
    pageController.initHistory();
    pageController.setBgClass(bg_class);
    pageController.initMenuHovers();
});